package com.eqdd.yiqidian.ui;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.eqdd.inputs.AndroidNextInputs;
import com.eqdd.inputs.LazyLoaders;
import com.eqdd.inputs.WidgetAccess;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.nextinputs.ValueScheme;
import com.eqdd.yiqidian.PhonePasswordGetActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class PhonePasswordGetActivity extends BaseActivity {
    private PhonePasswordGetActivityCustom dataBinding;
    private String phone;
    private AndroidNextInputs inputs;
    private WidgetAccess access;
    private String smsCode;
    private String msgid;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_phone_password_get);
        initTopTitleBar(View.VISIBLE, "密码找回");
        smsCode = getIntent().getStringExtra("smsCode");
    }

    @Override
    public void initData() {
        phone = getIntent().getStringExtra("phone");
        msgid = getIntent().getStringExtra("msgid");
        inputs = new AndroidNextInputs();
        access = new WidgetAccess(this);
        // 必选，与邮件相同
        LazyLoaders loader = new LazyLoaders(this);
        inputs
                .add(access.findEditText(R.id.et_password))
                .with(StaticScheme.Required(), ValueScheme.RangeLength(8, 18))// 必选 ，密码8~18
                .add(access.findEditText(R.id.et_password_again))
                .with(StaticScheme.Required(), ValueScheme.EqualsTo(loader.fromEditText(R.id.et_password)));
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_submit:
                if (inputs.test()) {
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.USER_PHONE_GET_PASSWORD)
                            .params("tel", phone)
                            .params("code", smsCode)
                            .params("msgid", msgid)
                            .params("password", EncryptUtil.sha1(dataBinding.etPassword.getText().toString() + "EQD"))
                            .execute(new DialogCallBack<HttpResult>(PhonePasswordGetActivity.this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        setResult(Config.SUCCESS);
                                        finish();
                                    }
                                }
                            });
                }
                break;
        }
    }
}
